package com.caseProcess.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.caseProcess.entity.*;
import com.caseProcess.service.PageService;
import com.caseProcess.service.VBeUrgingService;
import com.caseProcess.utils.PagingUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/vBeUrging")
public class VBeUrgingController {
    @Resource
    private VBeUrgingService vBeUrgingService;
    @Resource
    private PageService pageService;

    @RequestMapping("/getVBeUrgingByDepartment")
    public Result getVBeUrgingByDepartment(HttpServletRequest request,@RequestParam(required = false,defaultValue = "0")int page,
                                           @RequestParam(required = false,defaultValue = "0")int limit) {
        Result result=new Result();
        Object object=SessionController.getSession(request,"police");
        if(object==null){
            result.setCode(-500);
            result.setMsg("用户信息错误,请重新登陆");
            return result;
        }
        VUserInfo vUserInfo= (VUserInfo) object;
        VBeUrging vBeUrging=new VBeUrging();
        vBeUrging.setProcDepartmentId(vUserInfo.getDepartmentId());
        List<VBeUrging> list=vBeUrgingService.selectVBeUrgings(vBeUrging);
        if (list.size() >= 0){
            Map<String,VBeUrging> map=new HashMap<String, VBeUrging>();
            for (int i=0;i<list.size();i++){
                map.put(list.get(i).getProcessId(),list.get(i));//去除重复项
            }
            list.clear();
            for(String key:map.keySet()){
                list.add(map.get(key));
            }
            if(page!=0&&limit!=0){
                list= PagingUtil.paging(list,page,limit);
            }
            result.setCount(list.size());
            result.setMsg("操作成功");
            result.setData(list);
            return result;
        }else{
            result.setCount(0);
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }

    @RequestMapping("/getVBeUrgingByDepartmentOrTime")
    public Result getVBeUrgingByDepartmentOrTime(String caseId,String procLevel,String caseDescription,String startTime,String endTime,HttpServletRequest request,@RequestParam(required = false,defaultValue = "0")int page,
                                           @RequestParam(required = false,defaultValue = "0")int limit) {
        Result result=new Result();
        Object object=SessionController.getSession(request,"police");
        if(object==null){
            result.setCode(-500);
            result.setMsg("用户信息错误,请重新登陆");
            return result;
        }
        VUserInfo vUserInfo= (VUserInfo) object;
        VBeUrging vBeUrging=new VBeUrging();
        vBeUrging.setProcDepartmentId(vUserInfo.getDepartmentId());
        vBeUrging.setCaseId(caseId);
        vBeUrging.setProcLevel(procLevel);
        vBeUrging.setCaseDescription(caseDescription);
        List<VBeUrging> list=vBeUrgingService.selectVBeUrgingByDepartmentOrTime(vBeUrging,startTime,endTime);
        if (list.size() >= 0){
            Map<String,VBeUrging> map=new HashMap<String, VBeUrging>();
            for (int i=0;i<list.size();i++){
                map.put(list.get(i).getProcessId(),list.get(i));//去除重复项
            }
            list.clear();
            for(String key:map.keySet()){
                list.add(map.get(key));
            }
            if(page!=0&&limit!=0){
                list= PagingUtil.paging(list,page,limit);
            }
            result.setCount(list.size());
            result.setMsg("操作成功");
            result.setData(list);
            return result;
        }else{
            result.setCount(0);
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }

    /**
     *  查询所有催办(数据库分页)
     * @param selectConditionJson
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getAllVBeUrgings")
    public Result getAllVBeUrgings(String selectConditionJson, int page, int limit) {
        SelectCaseUrgingCondition selectCaseUrgingCondition = JSONObject.parseObject(selectConditionJson, SelectCaseUrgingCondition.class);
        String strWhere = "";
        strWhere+="1=1";
        if (!selectCaseUrgingCondition.getCaseId().equals("") && !selectCaseUrgingCondition.getCaseId().equals(null)) {
            strWhere += " and caseid like concat('%','" + selectCaseUrgingCondition.getCaseId() + "','%')";
        }
        if (!selectCaseUrgingCondition.getProcLevel().equals("") && !selectCaseUrgingCondition.getProcLevel().equals(null)) {
            strWhere += " and proclevel = '" + selectCaseUrgingCondition.getProcLevel()+"'";
        }
        if (!selectCaseUrgingCondition.getCaseDescription().equals("") && !selectCaseUrgingCondition.getCaseDescription().equals(null)) {
            strWhere += " and casedescription like concat('%','" + selectCaseUrgingCondition.getCaseDescription() + "','%')";;
        }
        if (!selectCaseUrgingCondition.getStartTime().equals("") && !selectCaseUrgingCondition.getStartTime().equals(null)) {

            strWhere += " and askForTime >='" + selectCaseUrgingCondition.getStartTime()+"'";
        }
        if (!selectCaseUrgingCondition.getEndTime().equals("") && !selectCaseUrgingCondition.getEndTime().equals(null)) {
            strWhere += " and askForTime <='" + selectCaseUrgingCondition.getEndTime()+"'";
        }

        //strWhere +="order by asktime desc";
        Page pg = new Page("V_CaseProcInfoByPolice", "caseid",1 , true, "*", limit, page, strWhere);

        List<Map<String, Object>> maps = pageService.selectByPage(pg);
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(maps);

        Result result=new Result();
        VBeUrging vBeUrging=new VBeUrging();
        List<VBeUrging> list=vBeUrgingService.selectVBeUrgings(vBeUrging);
        if (list.size() >= 0){
            Map<String,VBeUrging> map=new HashMap<String, VBeUrging>();
            for (int i=0;i<list.size();i++){
                map.put(list.get(i).getProcessId(),list.get(i));//去除重复项
            }
            list.clear();
            for(String key:map.keySet()){
                list.add(map.get(key));
            }
            result.setMsg("操作成功");
            result.setData(list);
            result.setCount(list.size());
            return result;
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }
}
